package com.daiyang.campus_lucky_draw.contreller;

import com.daiyang.campus_lucky_draw.common.error.ControllerErrorCodeConstants;
import com.daiyang.campus_lucky_draw.common.exception.ControllerException;
import com.daiyang.campus_lucky_draw.common.result.CommonReslut;
import com.daiyang.campus_lucky_draw.common.utils.JacksonUtil;
import com.daiyang.campus_lucky_draw.contreller.param.CreatePrizeParam;
import com.daiyang.campus_lucky_draw.contreller.param.PageListParam;
import com.daiyang.campus_lucky_draw.contreller.result.FindPrizeListResult;
import com.daiyang.campus_lucky_draw.service.PictureService;
import com.daiyang.campus_lucky_draw.service.PrizeService;
import com.daiyang.campus_lucky_draw.service.dto.PageListDTO;
import com.daiyang.campus_lucky_draw.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {
    private static final Logger logger =
            LoggerFactory.getLogger(PrizeController.class);
    @Autowired
    private PrizeService prizeService;
    /**
     * 创建奖品
     *
     * @param param
     * @param prizePic
     * @return
     */
    @RequestMapping("/prize/create")
    public CommonReslut<Long> createPrize(@Valid @RequestPart("param")
                                              CreatePrizeParam param,
                                            @RequestPart("prizePic")
                                          MultipartFile prizePic) {
        logger.info("createPrize param:{}",
                JacksonUtil.writeValueAsString(param));
        return CommonReslut.success(prizeService.createPrize(param, prizePic));
    }
    @Autowired
    PictureService pictureService;
    @RequestMapping("update/pic")
    public String testUpDataPic(@RequestPart("file") MultipartFile file) {
        logger.info("testUpDataPic file:{}",file);
        String fileName = pictureService.savePicture(file);
        return fileName;

    }
    @RequestMapping("/prize/find-list")
    public CommonReslut<FindPrizeListResult> findPrizeList(PageListParam
                                                                   param) {
        logger.info("findPrizeList PageListParam: " +
                JacksonUtil.writeValueAsString(param));
        PageListDTO<PrizeDTO> prizeListDTO = prizeService.findPrizeList(param);
        return
                CommonReslut.success(convertToFindPrizeListResult(prizeListDTO));
    }
    private FindPrizeListResult
    convertToFindPrizeListResult(PageListDTO<PrizeDTO> prizeListDTO) {
        if (null == prizeListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.PRIZES_FIND_ERROR);
        }
        FindPrizeListResult result = new FindPrizeListResult();
        result.setTotal(prizeListDTO.getTotal());
        result.setRecords(prizeListDTO.getRecords()
                .stream()
                .map(prizeDTO -> {
                    FindPrizeListResult.PrizeInfo prizeInfo = new
                            FindPrizeListResult.PrizeInfo();
                    prizeInfo.setPrizeId(prizeDTO.getId());
                    prizeInfo.setPrizeName(prizeDTO.getName());
                    prizeInfo.setDescription(prizeDTO.getDescription());
                    prizeInfo.setPrice(prizeDTO.getPrice());
                    // 添加/pic/前缀，与前端期望的路径格式一致
                    prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                    return prizeInfo;
                }).collect(Collectors.toList()));
        return result;
    }
}

